Polygon = function(x, y, theta, points, radius, color){
    
    this.location = new Point(x,y);
    this.angle = theta;
    this.points = points;
    this.radius = radius;
    
    if(color){
        this.color = color;
    }else{
        this.color = '#76EE00';
    }
    
    this.update = function(theta, location){
        this.angle = theta;
        this.location = location;
    };
    
    this.getAllSides = function(){
        var pointsCount = this.points.length;
        var sides = new Array();
        //p'x = cos(theta) * (px-ox) - sin(theta) * (py-oy) + ox
        //p'y = sin(theta) * (px-ox) + cos(theta) * (py-oy) + oy
        for(var i=0; i<pointsCount; i++){
            sides.push(new Array(this.points[i].add(this.location).rotate(this.angle, this.location), this.points[(i+1)%pointsCount].add(this.location).rotate(this.angle, this.location)));
        }
        return sides;
    };
    
    this.draw = function(context){
        context.save();
        //context.translate(this.location.x,this.location.y);
        //context.rotate(this.angle);
        context.beginPath();
        context.strokeStyle = this.color;
        context.lineWidth = 1;
        context.moveTo(this.location.x, this.location.y);
        if(this.points[0]){
            context.moveTo(this.points[0].add(this.location).rotate(this.angle, this.location).x, this.points[0].add(this.location).rotate(this.angle, this.location).y);
        }
        var pointsCount = this.points.length;
        for(var i=1; i<pointsCount; i++){
            context.lineTo(this.points[i].add(this.location).rotate(this.angle, this.location).x, this.points[i].add(this.location).rotate(this.angle, this.location).y);
        }
        context.lineTo(this.points[0].add(this.location).rotate(this.angle, this.location).x, this.points[0].add(this.location).rotate(this.angle, this.location).y);
        context.stroke();
        context.restore();
    };
    
    this.contains = function(point){
        if(Math.sqrt(Math.pow(point.x-this.location.x, 2)+Math.pow(point.y-this.location.y, 2)) > this.radius){
            return false;
        }
        //vertices is supposed a numpy.array,
        //or any structure wich has the minus operator as an element by element operation
        var vertices = this.getAllSides();
        var sign = 0;
        var n_vertices = vertices.length;
        for(var i=0; i<n_vertices; i++){
            //segment = vertices[n], vertices[(n+1)%n_vertices]
            var affine_segment = vertices[i][1].subtract(vertices[i][0]);
            var affine_point = point.subtract(vertices[i][0]);
            var cross_product = crossProduct(affine_segment, affine_point)
            cross_product /= Math.abs(cross_product); //normalized to 1 or -1
            if (sign == 0){ //the first case
                sign = cross_product ;
            }else if(cross_product != sign){
                return false;
            }
        }
        return true;
    };
}

crossProduct = function(point1, point2){
     return (point1.x*point2.y)-(point1.y*point2.x);
};
